var validator;
var $studentAddForm = $("#student-add-form");
var midUserName = $('#user_name').text();

function initMidUserName() {
    $('#midUserName').val(midUserName);
}
$(function() {
    validateRule();
    initMidUserName();

    $("#student-add .btn-save").click(function() {
        var name = $(this).attr("name");
        var validator = $studentAddForm.validate();
        var flag = validator.form();
        if (flag) {
            if (name == "save") {
                $.post(ctx + "midStudent/add", $studentAddForm.serialize(), function(r) {
                    if (r.code == 0) {
                        closeModal();
                        $MB.n_success(r.msg);
                        $MB.refreshTable("studentTable");
                    } else $MB.n_danger(r.msg);
                });
            }
            if (name == "update") {
                $.post(ctx + "midStudent/update", $studentAddForm.serialize(), function(r) {
                    if (r.code == 0) {
                        closeModal();
                        $MB.n_success(r.msg);
                        $MB.refreshTable("studentTable");
                    } else $MB.n_danger(r.msg);
                });
            }
        }
    });

    $("#student-add .btn-close").click(function() {
        closeModal();
    });

});

function closeModal() {
    $("#student-add-button").attr("name", "save");
    validator.resetForm();
    $MB.closeAndRestModal("student-add");
    $studentAddForm.find("input[name='studentname']").removeAttr("readonly");
    $("#student-add-modal-title").html('新增考生信息');
    $studentAddForm.find("input[name='status']").prop("checked",true);
}

function validateRule() {
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    validator = $studentAddForm.validate({
        rules: {
            idNumber: {
                required: true,
                minlength: 18,
                maxlength: 18,
                remote: {
                    url: "midStudent/checkIdNumber",
                    type: "get",
                    dataType: "json",
                    data: {
                        idNumber: function() {
                            return $("input[name='idNumber']").val().trim();
                        }
                    }
                }
            },
            midStudentId: {
                required: true,
                minlength: 12,
                maxlength: 12,
                remote: {
                    url: "midStudent/checkStudentId",
                    type: "get",
                    dataType: "json",
                    data: {
                        midStudentId: function() {
                            return $("input[name='midStudentId']").val().trim();
                        },
                        oldmidStudentId: function() {
                            return $("input[name='oldmidStudentId']").val().trim();
                        }
                    }
                }
            },
            name: {
                required: true
            },
            profession: {
                required: true
            },
            receiver: {
                required: true
            },
            direction: {
                required: true
            },
            graduateAdd: {
                required: true
            },
            email: {
                email: true
            },
            roles: {
                required: true
            },
            telephone: {
                required: true,
                checkPhone: true
            },
            ssex: {
                required: true
            }
        },
        errorPlacement: function(error, element) {
            if (element.is(":checkbox") || element.is(":radio")) {
                error.appendTo(element.parent().parent());
            } else {
                error.insertAfter(element);
            }
        },
        messages: {
            midStudentId: {
                required: icon + "请输入考生号",
                minlength: icon + "必须为12位考生号",
                maxlength: icon + "必须为12位考生号",
                remote: icon + "该考生号已经存在"
            },
            idNumber: {
                required: icon + "请输入身份证号",
                minlength: icon + "必须为18位身份证号",
                maxlength: icon + "必须为18位身份证号",
                remote: icon + "该身份证号已经存在"
            },
            telephone: {
                required: icon + "请输入手机号",
                checkPhone: icon + "请输入正确的手机号"
            },
            roles: icon + "请选择用户角色",
            email: icon + "邮箱格式不正确",
            name: icon + "请输入姓名",
            receiver: icon + "请输入收件人",
            direction: icon + "请输入收件人地址",
            profession: icon + "请选择专业",
            graduateAdd: icon + "请输入毕业初中",
            ssex: icon + "请选择性别"
        }
    });
}

